package com.study.bigwork.like.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.study.bigwork.entity.Like;
import com.study.bigwork.like.service.LikeService;

@Controller
@RequestMapping("/like")
public class LikeController {
	@Resource
	private LikeService likeService;
	
	@RequestMapping(value="/getLikes",method=RequestMethod.GET)
	public void getLikes(HttpServletRequest request,HttpServletResponse response) throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				// 设置返回数据格式和编码
				response.setContentType("application/json;charset=utf-8");

				// 定义StringBuffer变量
				String responseMessage = null;

				// 输出流
				PrintWriter out = response.getWriter();
				List<Like> likes=new ArrayList<>(); 
				System.out.println("getLikes    目前在diaoyongservice之前" );
				likes= likeService.getLikes();
				System.out.println("getLikes    " + Arrays.asList(likes));
				
				Gson gson = new Gson();
				responseMessage = gson.toJson(likes);
				System.out.println("对象转为json " + responseMessage);
				// 输出流将信息返回
				out.print(responseMessage);
	}
	@RequestMapping(value="/deleteLike",method=RequestMethod.POST)
	public void deleteLike(HttpServletRequest request,HttpServletResponse response) throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				// 设置返回数据格式和编码
				response.setContentType("application/json;charset=utf-8");
				BufferedReader br = request.getReader();
				String str= br.readLine();
				int i=Integer.parseInt(str);
				likeService.deleteLike(i);
				System.out.println("获取的id啊啊啊啊啊啊啊啊啊啊啊"+i);
	}

	@RequestMapping(value="/addLike",method=RequestMethod.POST)
	public void addLike(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		// 设置返回数据格式和编码
		response.setContentType("application/json;charset=utf-8"); 
		BufferedReader br = request.getReader();
		String str= br.readLine();
		List<String> a=Arrays.asList(str.split(","));
		String a1=a.get(0);
		String a2=a.get(1);
		int userid=Integer.parseInt(a1);
		int circleid=Integer.parseInt(a2);

		likeService.addLike(userid,circleid);
	//	System.out.println("获取的id啊啊啊啊啊啊啊啊啊啊啊"+a);
	}
	@RequestMapping(value="/cancelLikeCircle",method=RequestMethod.POST)
	public void cancelLikeCircle(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		// 设置返回数据格式和编码
		response.setContentType("application/json;charset=utf-8");
		BufferedReader br = request.getReader();
		String str= br.readLine();
		String[] a=str.split(",");
		int circleid=Integer.parseInt(a[0]);
		int count=Integer.parseInt(a[1]);

		likeService.changeLikeSize(circleid, count);
		System.out.println("获取的id啊啊啊啊啊啊啊啊啊啊啊"+a);
	}
	@RequestMapping(value="/addLikeCircle",method=RequestMethod.POST)
	public void addLikeCircle(HttpServletRequest request,HttpServletResponse response) throws IOException{
	
	// TODO Auto-generated method stub
			request.setCharacterEncoding("utf-8");
			// 设置返回数据格式和编码
			response.setContentType("application/json;charset=utf-8");
			BufferedReader br = request.getReader();
			String str= br.readLine();
			System.out.println("aa"+str);
			List<String> a=Arrays.asList(str.split(","));
			String a1=a.get(0);
			String a2=a.get(1);
			int circleid=Integer.parseInt(a1);
			int count=Integer.parseInt(a2);

			likeService.changeLikeSize(circleid,count);
			System.out.println("aaaaaaaaaa"+count);
	}
	
}
